<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\XApiCompany;
use App\Model\ZaXApiCompany;
use App\Service\UserService;
use App\Services\CompanyAttendanceRedisCacheService;
use FooRedis;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
 * @Command
 */
class AttendanceCache extends HyperfCommand
{

    /**
     * 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象
     *
     * @Inject
     * @var \App\FooRedis
     */
    private $redis;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    private $container;
    public function __construct(ContainerInterface $container,LoggerFactory $loggerFactory)
    {
        parent::__construct('demo:command');
        $this->logger = $loggerFactory->get('log', 'default');
        $this->container = $container;
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Hyperf Demo Command');
    }

    /**
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function handle()
    {

        $updateDate = '2000-01-01 00:00:00';
        $redisKey = $this->redis->bsKey['TABLE_COMPANY_UPDATE_DATE'];
        $redisCheckKey = $this->redis->bsKey['TABLE_COMPANY_CHECK'];
        if ($this->redis->get($redisCheckKey) == 1)
            return false;

        $this->redis->set($redisCheckKey,1);
        if ($this->redis->exists($redisKey))
            $updateDate = $this->redis->get($redisKey);

        $this->logger->info("key" .$redisKey);
        $companies = ZaXApiCompany::query()->where("update_date",">",$updateDate)->orderBy("update_date","ASC")->get();
        if (count($companies) > 0){
            foreach ($companies as $company){
                try{
                    $xrCompany = XApiCompany::query()->findOrFail($company->id1);
                    var_dump($company->update_date);
                    var_dump($xrCompany->update_date);
                    if ($xrCompany->update_date < $company->update_date){
                        if ($xrCompany->status != $company->status){
                            $oldCompanyStatus = $xrCompany->status;
                            $xrCompany->status = $company->status;
                            $xrCompany->update_date = $company->update_date;
                            $xrCompany->save();
//                            $this->logger->info("场所状态被更新 场所id:{},修改前的场所状态{},修改后的场所状态{}");
                        }
                    } //当主库的更新时间小于内网的更新时间，说明内网有修改


                }catch (ModelNotFoundException $exception){
                    $this->logger->info("不存在的companyId" .$company->id1);
                }
                $newUpdateDate = $company->update_date;
            }
            $this->logger->info("设置最新时间" .$newUpdateDate);
            $this->redis->set($redisKey,$newUpdateDate);
        }

        $this->redis->set($redisCheckKey,0);

    }
    protected function getArguments()
    {
        return [
            ['date', InputArgument::OPTIONAL, '这里是对这个参数的解释']
        ];
    }
}
